<?php

/* explain:		authority class
 * Project:     marsems
 * File:        Authority.php
 *
 * @ link 		http://www.marsems.com/lib
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	ivan.ling
 * @ version 1.0
 * 
 */
include_once("utils/CommonUtils.php");

class AuthImpl extends CommonUtils
{
	
	function __construct($DBTag = 'system')
	{
		parent::__construct();
		
		$this->DBTag 	= $DBTag;
	}
		
	public function getMenuFromUserID($userid)
	{		
		if($userid == 1)
			$sql = "SELECT g.id as gid, m.id as mid, g.name as gname, m.name as mname, m.url FROM menu_groups g, menu m WHERE g.id = m.groupid";
		else
			$sql = "SELECT g.id as gid, m.id as mid, g.name as gname, m.name as mname, m.url FROM authority a, menu_groups g, menu m WHERE g.id = m.groupid AND m.id = a.menuid AND a.userid = '$userid'";
		
		$menus 	= array();
		$conn 	= null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);			
			
			while($row = mysql_fetch_array($rs)){
				$menus[$row['gid']][] = $row;
			}			
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}	
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $menus;
	}
	
	public function getMenuFromGroupID($groupid)
	{		
		if($groupid == 1)
			$sql = "SELECT g.id as gid, m.id as mid, g.name as gname, m.name as mname, m.url FROM menu_groups g, menu m WHERE g.id = m.groupid ORDER BY g.orders, m.orders";
		else
			$sql = "SELECT g.id as gid, m.id as mid, g.name as gname, m.name as mname, m.url FROM group_authority a, menu_groups g, menu m WHERE g.id = m.groupid AND m.id = a.menuid AND a.groupid = '$groupid' ORDER BY g.orders, m.orders";
		
		$menus 	= array();
		$conn 	= null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);	
			$rs 	= mysql_query($sql, $conn);// or die (mysql_error($this->conHandle).$sql);
			
			while($row = mysql_fetch_array($rs)){
				$menus[$row['gid']][] = $row;
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		return $menus;
	}
	
	
	
	/* permission list
	 *
	 * @ param  int 	userid   	
	 * @ param  return 	array		data array
	 */
	public function listPermission($userid)
	{
		if(empty($userid))
			return false;
		
		$sql = "SELECT m.id as mid, m.name as name, m.remark as remark, a.operate as operate FROM menu m left join authority a on (m.id = a.id AND a.userid = $userid)";
		
		return $this->getRows($this->DBTag, $sql);		
	}
	
	/* permission list
	 *
	 * @ param  int 	userid   	 
	 * @ param  return 	array		data array
	 */
	public function listGroupPermission($groupid)
	{
		$sql = "SELECT m.id as mid, m.name as name, m.remark as remark, a.menuid as authMenuID, a.operate as operate FROM menu m left join group_authority a on (m.id = a.menuid AND a.groupid = $groupid)";
		
		return $this->getRows($this->DBTag, $sql);
	}
	
	public function delAuthority($userid)
	{		
		$sql = "DELETE FROM authority WHERE userid = $userid'";
		
		$this->update($this->DBTag, $sql);
	}
	
	public function addAuthority($userid, $menuid, $operate)
	{
		$sql = "INSERT INTO authority SET userid = '$userid', menuid='$menuid', operate='$operate'";
		
		return $this->insert($this->DBTag, $sql);		
	}

	public function addAuthTransaction($userid, $arrOperateCache)
	{
		$sql = 'set session autocommit=false';
		$this->update($this->DBTag, $sql);		
		
		$sql = 'start transaction';
		$this->update($this->DBTag, $sql); 
		
		$sql = "DELETE FROM authority WHERE userid = '$userid'";		
		$this->update($this->DBTag, $sql);
		
		foreach ($arrOperateCache as $menuid => $arrOperation){
			$sql	= "INSERT INTO authority SET userid = '$userid', menuid='$menuid', operate='" . implode(',', $arrOperation) . "'";
			
			$rs 	= $this->insert($this->DBTag, $sql);
			if(!$rs){
				$sql = 'rollback';
				$this->update($this->DBTag, $sql);
				
				break;
			}
		}
		
		$sql = 'commit';
		$rs = $this->update($this->DBTag, $sql);
		if(!$rs){
			$sql 	= 'rollback';
			$rs		= $this->update($this->DBTag, $sql);
		}
		
		$sql 	= 'set session autocommit=true';
		$rs 	= $this->update($this->DBTag, $sql);
		
		return $rs ? true: false;
	}
	
	public function addGroupAuthTransaction($groupid, $arrOperateCache)
	{
		$sql = 'set session autocommit=false';
		$this->updateDdl($this->DBTag, $sql);
		
		$sql = 'start transaction';
		$this->updateDdl($this->DBTag, $sql);
		
		$sql = "DELETE FROM group_authority WHERE groupid = '$groupid'";
		$this->update($this->DBTag, $sql);
		
		foreach ($arrOperateCache as $menuid => $arrOperation){
			$sql 	= "INSERT INTO group_authority SET groupid = '$groupid', menuid='$menuid', operate='" . implode(',', $arrOperation) . "'";
			$rs 	= $this->insert($this->DBTag, $sql);
			if(!$rs){
				$sql = 'rollback';
				$this->update($this->DBTag, $sql);
				break;
			}
		}
		
		$sql 	= 'commit';
		$rs 	= $this->updateDdl($this->DBTag, $sql);
		if(!$rs){
			$sql = 'rollback';
			$this->updateDdl($this->DBTag, $sql);
		}
		
		$sql 	= 'set session autocommit=true';
		$rs 	= $this->updateDdl($this->DBTag, $sql);
		
		return $rs ? true: false;
	}
}
?>
